package com.tanhua.admin.module.loginIndex.controller;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.admin.module.loginIndex.service.Impl.LoginIndexServiceImpl;
import com.tanhua.admin.module.loginIndex.vo.LoginIndexVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author CZY
 * @Date 2021/4/20 10:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/dashboard")
@Slf4j
public class LonginIndexComtroller {

    @Autowired
    private LoginIndexServiceImpl loginIndexService;

    @Autowired
    private final static ObjectMapper MAPPER=new ObjectMapper();

    /**
     * 概要信息请求端口
     *
     * @param token
     * @return
     */
    @GetMapping("/summary")
    public ResponseEntity<LoginIndexVo> loginIndexVo(@RequestHeader("Authorization") String token) {
        try {
            LoginIndexVo loginIndexVo = loginIndexService.queryLogin(token);
            if (null == loginIndexVo) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            return ResponseEntity.ok(loginIndexVo);
        } catch (Exception e) {
            log.error("查询首页信息出错了  logimIdex", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 新增、活跃用户、次日留存率请求
     * @param token 指令
     * @param startTime 开始时间,传入类型为long
     * @param endTime  结束时间,传入类型为long
     * @param retentionRate  操作类型101,102,103
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity<Map<String,Object[]>> queryParam(@RequestHeader("Authorization") String token,
                                                                    @RequestParam(value = "sd") Long startTime,
                                                                    @RequestParam(value = "ed") Long endTime,
                                                                    @RequestParam(value = "type") Integer retentionRate) {

        try {
            Map<String, Object[]> map = loginIndexService.queryParam(token,startTime,endTime,retentionRate);
            if (CollUtil.isEmpty(map)) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            return ResponseEntity.ok(map);
        } catch (Exception e) {
            log.error("查询首页信息出错了  logimIdex", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 分析分布统计请求接口
     * @param token 指令
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @return
     */
    @GetMapping("/distribution")
    public ResponseEntity<Map<String,Object[]>> loginAnalyze(@RequestHeader("Authorization") String token,
                                                             @RequestParam(value = "sd") Long startTime,
                                                             @RequestParam(value = "ed") Long endTime
                                                             ){
        try {
            Map<String,Object[]> map=loginIndexService.queryInfo(startTime,endTime);
            if (CollUtil.isEmpty(map)){
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            return ResponseEntity.ok(map);
        }catch (Exception e){
            log.error("分析分布统计查询出错了 ~",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
